/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original Code is
*   Nick Kreeger
* Portions created by the Initial Developer are Copyright (C) 2006-2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*   Nick Kreeger <nick.kreeger@park.edu>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */

#import "NewAccountController.h"
#import "MessageAccountManager.h"

@interface NewAccountController(Private)

-(NSString*)literalForAccountType:(EAccountType)aAccountType;
-(id<NewAccountView>)currentNewAccountView;
-(void)finishAccount;

@end

@implementation NewAccountController

static NewAccountController* sNewAccountControllerInstance = nil;

+(NewAccountController*)sharedInstance
{
  if (!sNewAccountControllerInstance)
    sNewAccountControllerInstance = [[self alloc] init];
  
  return sNewAccountControllerInstance;
}

-(id)init
{
  if ((self = [super init]))
  {
    mMessageAccount = [[MessageAccount alloc] init];
    [NSBundle loadNibNamed:@"NewAccountDialog" owner:self];
  }
  
  return self;
}

-(void)dealloc
{
  [mMessageAccount release];
  [super dealloc];
}

-(void)awakeFromNib
{
  [mSetupView addSubview:mIdentityView];
  mCurSetupViewIndex = eIdentityViewIndex;
}

-(void)runNewAccountDialog:(NSWindow*)aParentWindow
{
  // Make sure our dialogs are clear
  [mIdentityView clearValues];
  [mServerInfoView clearValues];
  [mUserNamesView clearValues];
  [mAccountNameView clearValues];
  [mAccountSummaryView clearValues];
  
  // Make sure the current dialog is the identity view.
  if (mCurSetupViewIndex != eIdentityViewIndex)
  {
    NSView<NewAccountView>* curView = [self currentNewAccountView];
    [curView removeFromSuperview];
    [mSetupView addSubview:mIdentityView];
    mCurSetupViewIndex = eIdentityViewIndex;
  }
  
  [NSApp beginSheet:mWindow
     modalForWindow:aParentWindow 
      modalDelegate:self
     didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
        contextInfo:nil];
}

-(void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
  [sheet orderOut:self];
}

-(IBAction)cancel:(id)sender
{
  [NSApp endSheet:[sender window]];
}

-(IBAction)goBack:(id)sender
{
  NSView<NewAccountView>* curView = [self currentNewAccountView];
  if (!curView || ![curView canGoBackward])
    return;
  
  NSView<NewAccountView>* prevView = nil;
  switch (mCurSetupViewIndex)
  {
    case eServerInfoViewIndex:
      prevView = mIdentityView;
      mCurSetupViewIndex = eIdentityViewIndex;
      [mGoBackButton setEnabled:NO];
      break;
      
    case eUserNamesViewIndex:
      prevView = mServerInfoView;
      mCurSetupViewIndex = eServerInfoViewIndex;
      break;
      
    case eAccountNameViewIndex:
      prevView = mUserNamesView;
      mCurSetupViewIndex = eUserNamesViewIndex;
      break;
      
    case eAccountSummaryViewIndex:
      prevView = mAccountNameView;
      mCurSetupViewIndex = eAccountNameViewIndex;
      [mContinueButton setTitle:@"Continue"]; //XXX Localize
      break;
  }
  
  [curView removeFromSuperview];
  [mSetupView addSubview:prevView];
}

-(IBAction)doContinue:(id)sender
{
  NSView<NewAccountView>* curView = [self currentNewAccountView];
  if (!curView || ![curView canGoForward])
    return;
  
  NSView<NewAccountView>* nextView = nil;
  switch (mCurSetupViewIndex)
  {
    case eIdentityViewIndex:
      nextView = mServerInfoView;
      mCurSetupViewIndex = eServerInfoViewIndex;
      [mGoBackButton setEnabled:YES];
      break;
      
    case eServerInfoViewIndex:
      nextView = mUserNamesView;
      mCurSetupViewIndex = eUserNamesViewIndex;
      break;
      
    case eUserNamesViewIndex:
      nextView = mAccountNameView;
      mCurSetupViewIndex = eAccountNameViewIndex;
      break;
      
    case eAccountNameViewIndex:
      nextView = mAccountSummaryView;
      mCurSetupViewIndex = eAccountSummaryViewIndex;
      [mAccountSummaryView setAccountName:[mAccountNameView accountName]];
      [mAccountSummaryView setEmailAddress:[mIdentityView emailAddress]];
      [mAccountSummaryView setIncomingUsername:[mUserNamesView incomingUsername]];
      [mAccountSummaryView setIncomingServerName:[mServerInfoView incomingServerName]];
      [mAccountSummaryView setOutgoingUsername:[mUserNamesView outgoingUsername]];
      [mAccountSummaryView setOutgoingServerName:[mServerInfoView outgoingServerName]];
      [mAccountSummaryView setIncomingServerType: ([self literalForAccountType:[mServerInfoView accountType]])];
      
      [mContinueButton setTitle:@"Finish"]; // XXX Localize
      break;
      
    case eAccountSummaryViewIndex:
      [mContinueButton setTitle:@"Finishing"]; //XXX Localize
      [mContinueButton setEnabled:NO];
      [self finishAccount];
      [NSApp endSheet:mWindow];
      break;
  }
  
  [curView removeFromSuperview];
  [mSetupView addSubview:nextView];
}

-(id<NewAccountView>)currentNewAccountView
{
  id<NewAccountView> curAccountView = nil;
  
  switch (mCurSetupViewIndex)
  {
    case eIdentityViewIndex:
      curAccountView = mIdentityView;
      break;
      
    case eServerInfoViewIndex:
      curAccountView = mServerInfoView;
      break;
      
    case eUserNamesViewIndex:
      curAccountView = mUserNamesView;
      break;
      
    case eAccountNameViewIndex:
      curAccountView = mAccountNameView;
      break;
      
    case eAccountSummaryViewIndex:
      curAccountView = mAccountSummaryView;
      break;
  }
  
  return curAccountView;
}

-(NSString*)literalForAccountType:(EAccountType)aAccountType
{
  NSString* retVal = nil;  // this _might_ piss off the text field
  
  switch (aAccountType)
  {
    case ePOP3:
      retVal = @"POP3";
      break;
      
    case eIMAP:
      retVal = @"IMAP";
      break;
  }
  
  return retVal;
}

-(void)finishAccount
{
  MessageAccount* newAccount = [[MessageAccount alloc] init];
  [newAccount setAccountName:[mAccountNameView accountName]];
  [newAccount setFullName:[mIdentityView fullName]];
  [newAccount setEmailAddress:[mIdentityView emailAddress]];
  
  [newAccount setIncomingServerUserName:[mUserNamesView incomingUsername]];
  [newAccount setIncomingServerName:[mServerInfoView incomingServerName]];
  [newAccount setIncomingServerType:[mServerInfoView accountType]];
  
  [newAccount setOutgoingServerUserName:[mUserNamesView outgoingUsername]];
  [newAccount setOutgoingServerName:[mServerInfoView outgoingServerName]];
  [newAccount setOutgoingServerType:eSMTP];
  
  [[MessageAccountManager sharedInstance] createAccount:newAccount];
  
  [newAccount release];
}

@end

#pragma mark -

@implementation IdentityView

-(NSString*)fullName
{
  return [mNameField stringValue];
}

-(NSString*)emailAddress
{
  return [mAddressField stringValue];
}

-(BOOL)canGoForward
{
  BOOL canContinue = YES;
  
  if ([[mNameField stringValue] isEqualToString:@""])
  {
    [mErrorField setStringValue:@"Please enter your name"]; //XXX Localize
    canContinue = NO;
  }
  else if ([[mAddressField stringValue] isEqualToString:@""])
  {
    [mErrorField setStringValue:@"Please enter your email address"]; //XXX Localize
    canContinue = NO;
  }
  
  //XXX check for '@' in the email address.
  
  if (canContinue)
    [mErrorField setStringValue:@""];
  
  return canContinue;
}

-(BOOL)canGoBackward
{
  return YES;
}

-(void)clearValues
{
  [mErrorField setStringValue:@""];
  [mNameField setStringValue:@""];
  [mAddressField setStringValue:@""];
}

@end

#pragma mark -

@implementation ServerInfoView

-(NSString*)incomingServerName
{
  return [mIncomingServerField stringValue];
}

-(NSString*)outgoingServerName
{
  return [mOutgoingServerField stringValue];
}

-(BOOL)shouldUseGlobalInbox
{
  return [mUseGlobalInboxButton state];
}

-(EAccountType)accountType
{
  return (EAccountType)[mServerTypeMatrix selectedTag];
}

-(BOOL)canGoForward
{
  BOOL canContinue = YES;
  
  if ([[mIncomingServerField stringValue] isEqualToString:@""])
  {
    [mErrorField setStringValue:@"Please enter your incoming server name"];  //XXX Localize
    canContinue = NO;
  }
  else if ([[mOutgoingServerField stringValue] isEqualToString:@""])
  {
    [mErrorField setStringValue:@"Please enter your outgoing server name"];  //XXX Localize
    canContinue = NO;
  }
  
  if (canContinue)
    [mErrorField setStringValue:@""];
  
  return canContinue;
}

-(BOOL)canGoBackward
{
  return YES;
}

-(void)clearValues
{
  [mErrorField setStringValue:@""];
  [mIncomingServerField setStringValue:@""];
  [mOutgoingServerField setStringValue:@""];
  [mUseGlobalInboxButton setState:NSOffState];
  [mServerTypeMatrix selectCellWithTag:(int)ePOP3];
}

@end

#pragma mark -

@implementation UserNamesView

-(NSString*)incomingUsername
{
  return [mIncomingUserNameField stringValue];
}

-(NSString*)outgoingUsername
{
  return [mOutgoingUserNameField stringValue];
}

-(BOOL)canGoForward
{
  BOOL canContinue = YES;
  
  if ([[mIncomingUserNameField stringValue] isEqualToString:@""])
  {
    [mErrorField setStringValue:@"Please enter your incoming username"];  //XXX Localize
    canContinue = NO;
  }
  
  else if ([[mOutgoingUserNameField stringValue] isEqualToString:@""])
  {
    [mErrorField setStringValue:@"Please enter your outgoing username"];  //XXX Localize
    canContinue = NO;
  }
  
  if (canContinue)
    [mErrorField setStringValue:@""];
  
  return canContinue;
}

-(BOOL)canGoBackward
{
  return YES;
}

-(void)clearValues
{
  [mErrorField setStringValue:@""];
  [mIncomingUserNameField setStringValue:@""];
  [mOutgoingUserNameField setStringValue:@""];
}

@end

#pragma mark -

@implementation AccountNameView

-(NSString*)accountName
{
  return [mAccountNameField stringValue];
}

-(BOOL)canGoForward
{
  if ([[mAccountNameField stringValue] isEqualToString:@""])
  {
    [mErrorField setStringValue:@"Please enter an account name"];  //XXX Localize
    return NO;
  }
  
  [mErrorField setStringValue:@""];
  return YES;
}

-(BOOL)canGoBackward
{
  return YES;
}

-(void)clearValues
{
  [mErrorField setStringValue:@""];
  [mAccountNameField setStringValue:@""];
}

@end

#pragma mark -

@implementation AccountSummaryView

-(void)setAccountName:(NSString*)aAccountName
{
  [mAccountName setStringValue:aAccountName];
}

-(void)setEmailAddress:(NSString*)aEmailAddress
{
  [mEmailAddress setStringValue:aEmailAddress];
}

-(void)setIncomingUsername:(NSString*)aIncomingUsername
{
  [mIncomingUsername setStringValue:aIncomingUsername];
}

-(void)setIncomingServerName:(NSString*)aIncomingServerName
{
  [mIncomingServerName setStringValue:aIncomingServerName];
}

-(void)setIncomingServerType:(NSString*)aIncomingServerType
{
  [mIncomingServerType setStringValue:aIncomingServerType];
}

-(void)setOutgoingUsername:(NSString*)aOutgoingUsername
{
  [mOutgoingUserName setStringValue:aOutgoingUsername];
}

-(void)setOutgoingServerName:(NSString*)aServerName
{
  [mOutgoingServerName setStringValue:aServerName];
}

-(BOOL)shouldDownloadMessagesNow
{
  return [mDownloadMessagesNowButton state];
}

-(BOOL)canGoForward
{
  return YES;
}

-(BOOL)canGoBackward
{
  return YES;
}

-(void)clearValues
{
  [mAccountName setStringValue:@""];
  [mEmailAddress setStringValue:@""];
  [mIncomingUsername setStringValue:@""];
  [mIncomingServerName setStringValue:@""];
  [mIncomingServerType setStringValue:@""];
  [mOutgoingUserName setStringValue:@""];
  [mOutgoingServerName setStringValue:@""];
  [mDownloadMessagesNowButton setState:NSOnState];
}

@end
